package navigation.wzg.com.navigation.http;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

import navigation.wzg.com.navigation.R;
import navigation.wzg.com.navigation.Write.Shareds;
import navigation.wzg.com.navigation.http.Eentity.Eentity;
import navigation.wzg.com.navigation.http.Interface.Add_http;
import navigation.wzg.com.navigation.http.Interface.Login_http;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by WZG on 2017/10/26.
 */

public class Http_method {
    private static int i=0;
    public static int state(String editInfo, String editPwd, final Context context){
        Login(editInfo,editPwd,context);
        return i;
    }
    public static void Login(String editInfo, String editPwd, final Context context){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://114.67.157.123/servlet/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Login_http service = retrofit.create(Login_http.class);//获取API接口的实现类的实例对象
        Call<Eentity> gsonnn = service.getResult(editInfo,editPwd);
        gsonnn.enqueue(new Callback<Eentity>() {
            @Override
            public void onResponse(Call<Eentity> call, Response<Eentity> response) {
                Eentity gsont = response.body();
                String aaa = gsont.getLogin();
                if (aaa.equals("200")){
                    i = 200;
                    Toast.makeText(context, gsont.getLogin(), Toast.LENGTH_SHORT).show();
                    Shareds.First("item",false,context);
                }else {
                    Toast.makeText(context, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<Eentity> call, Throwable t) {
                Toast.makeText(context, "请检查网络连接", Toast.LENGTH_SHORT).show();
            }
        });
    }


    public static void ADD(final String user, final String pwd, final Activity context){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://114.67.157.123/servlet/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Add_http service = retrofit.create(Add_http.class);//获取API接口的实现类的实例对象
        Call<Eentity> gsonnn = service.getResult1(user,pwd);
        gsonnn.enqueue(new Callback<Eentity>() {
            @Override
            public void onResponse(Call<Eentity> call, Response<Eentity> response) {
                Eentity aaa = response.body();

                if (aaa.getInsert()==200){
                    new AlertDialog.Builder(context)
                            .setTitle("欢迎使用")
                            .setMessage("您已成功注册账号")
                            .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                   context.finish();
                                   context.overridePendingTransition(R.anim.out, R.anim.up);
                                }
                            }).show();
                }else {
                    Toast.makeText(context, "注册失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Eentity> call, Throwable t) {
                Toast.makeText(context, "no", Toast.LENGTH_SHORT).show();
            }
        });
    }


}
